﻿using Data.Context;
using Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data.Logic
{
    public class UsersLogic
    {
        public UsersModel ToModel(UsersEntity entity)
        {
            return entity != null ? new UsersModel { 
                avatar=entity.avatar,comments=entity.comments,datecreate=entity.datecreate,datemodified=entity.datemodified,email=entity.email,
                fullname=entity.fullname,id=entity.id,is_fb_login=entity.is_fb_login,is_gg_login=entity.is_gg_login,is_tweet_login=entity.is_tweet_login,isactive=entity.isactive,last_login=entity.last_login,password=entity.password,zuicoins=entity.zuicoins,zuiorder=entity.zuiorder
            } : null;
        }
        public bool Insert(UsersEntity entity,out int userId,out string message)
        {
            bool result = false;
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objUser = db.Users.Where(a => a.email == entity.email).FirstOrDefault();
                    if (objUser == null)
                    {
                        db.Users.AddObject(entity);
                        db.SaveChanges();
                        userId = entity.id;
                        message = "";
                        result = true;
                    }
                    else
                    {
                        result = false;
                        userId = 0;
                        message = "Đã tồn tại email.";
                    }
                    return result;
                }


            }
            catch
            {
                userId = 0;
                message = "Lỗi";
                return false;
            }
        }

        public bool Update(UsersEntity entity)
        {
            bool result = false;
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objUser = db.Users.Where(a => a.email == entity.email).FirstOrDefault();
                    if (objUser != null)
                    {
                        db.Users.Attach(entity);
                        db.SaveChanges();
                      
                        result = true;
                    }
                    else
                    {
                        result = false;
                      
                    }
                    return result;
                }


            }
            catch
            {
              
                return false;
            }
        }

        public UsersEntity getUserInfo(string email)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objUser = db.Users.Where(a => a.email == email).FirstOrDefault();
                    return objUser;
                }
            }
            catch (Exception)
            {

                return null;
            }
        }

        public UsersEntity getUserInfoById(int userId)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objUser = db.Users.Where(a => a.id == userId).FirstOrDefault();
                    return objUser;
                }
            }
            catch (Exception)
            {

                return null;
            }
        }
    }
}
